//
//  RankingViewController.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/5/2.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "RankingViewController.h"
#import "TenGrayShadowView.h"
#import "CommonStudyLinkHeaderView.h"
#import "StudyRankingTableViewCell.h"
#import "StudyRankingPersonInformationViewController.h"
#import "LpRankingPersonModel.h"
@interface RankingViewController ()<UITableViewDelegate,UITableViewDataSource>
/**  table  */
@property (strong, nonatomic) UITableView  *tableView;

/**  数据  */
@property (strong, nonatomic) NSMutableArray  *dataSouce;
/**  第一名数据  */
@property (strong, nonatomic) LpRankingPersonModel  *firstModel;
/**  我的数据  */
@property (strong, nonatomic) LpRankingPersonModel  *myModel;
/**  header  */
@property (strong, nonatomic) CommonStudyLinkHeaderView  *header;
/**  判断是否是第一次  */
@property (assign, nonatomic) BOOL   isNoFirst;

/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
/**  页数  */
@property (assign, nonatomic) NSInteger  page;



@end

@implementation RankingViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.page = 1;
    [self netWorking_header];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.titlelabel.text = @"排行";
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
        
    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];
    [self layoutUI];
}





#pragma mark - 数据
//下拉刷新
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    if (self.isNoFirst == false) {
        [YJProgressHUD showMessage:@"加载中..." duration:20 view:self.view];
    }
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    [paramer setObject:ZJUSERINFORMODEL.ORG_ACCOUNT.ID forKey:@"ACCOUNTID"];
    [paramer setObject:[NSString stringWithFormat:@"%ld",self.page] forKey:@"START_INDEX"];
    [paramer setObject:@"20" forKey:@"PAGE_SIZE"];
    [NetWorkingManager postWithUrlString:[NetWorkingURL getFriendRanking] Parameter:paramer Success:^(id object) {
        [YJProgressHUD hide];
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            weakSelf.dataSouce = [[NSArray yy_modelArrayWithClass:[LpRankingPersonModel class] json:[object objectForKey:@"ORD"]] mutableCopy];
            weakSelf.myModel = [LpRankingPersonModel yy_modelWithJSON:[object objectForKey:@"ME_ORD"]];
            for (LpRankingPersonModel *model  in weakSelf.dataSouce) {
                if ([model.ORDERBY isEqualToString:@"1"]) {
                    weakSelf.header.model = model;
                }
            }
            [weakSelf.tableView reloadData];
            self.isNoFirst = YES;
        }else{
            if (self.isNoFirst == false) {
                [LYEmptyManager showLoadFailWithView:weakSelf.tableView managerBlock:^{
                    [weakSelf netWorking_header];
                }];
            }
            
        }
        [self.tableView.mj_header endRefreshing];
    } Fail:^(id object) {
        [self.tableView.mj_header endRefreshing];
        [YJProgressHUD hide];
        if (self.isNoFirst == false) {
            [LYEmptyManager showLoadFailWithView:weakSelf.tableView managerBlock:^{
                [weakSelf netWorking_header];
            }];
        }
        
    }];
    
    
}

//上拉加载
- (void)netWorking_footer{
    __weak typeof(self) weakSelf = self;
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    [paramer setObject:ZJUSERINFORMODEL.ORG_ACCOUNT.ID forKey:@"ACCOUNTID"];
    [paramer setObject:[NSString stringWithFormat:@"%ld",self.page] forKey:@"START_INDEX"];
    [paramer setObject:@"20" forKey:@"PAGE_SIZE"];
    [NetWorkingManager postWithUrlString:[NetWorkingURL getFriendRanking] Parameter:paramer Success:^(id object) {
        [YJProgressHUD hide];
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = [object objectForKey:@"ORD"];
            
            if (array.count == 0) {
                _page --;
            }else{
                [weakSelf.dataSouce addObjectsFromArray:[NSArray yy_modelArrayWithClass:[LpRankingPersonModel class] json:array]] ;
                [weakSelf.tableView reloadData];
            }
            
        }
        
        [weakSelf.tableView.mj_footer endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
        
    }];
    
}




#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.tableView];
    self.tableView.sd_layout
    .leftEqualToView(self.view)
    .rightEqualToView(self.view)
    .topSpaceToView(self.navigView, 0)
    .bottomSpaceToView(self.view, LP_TabBarHeight);
    CommonStudyLinkHeaderView *header = [[CommonStudyLinkHeaderView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 220 * [UIScreen zj_screenHScale])];
    header.isName = NO;
    self.tableView.tableHeaderView = header;
    self.header = header;
    
}


#pragma mark - 私有方法
- (void)action_leftButton:(UIButton *)sender{
    [[ToolClass getMainNavigationController] popViewControllerAnimated:YES];
}

#pragma mark - 监听事件


#pragma mark - 协议代理

/**  UITableViewDataSource,UITableViewDelegate  */
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else {
        return self.dataSouce.count;
    }
    
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        
        return 65 * [UIScreen zj_screenHScale];
    }else {
        return 55 * [UIScreen zj_screenHScale];
    }
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *weizhi = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    StudyRankingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
    if (!cell) {
        cell = [[StudyRankingTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
    }
    
    cell.section = indexPath.section;
    cell.row = indexPath.row;
    if (indexPath.section == 0) {
        cell.model = self.myModel;
    }else{
        cell.model = self.dataSouce[indexPath.row];
    }
    
    return cell;
}
/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section != 0) {
        
        LpRankingPersonModel *model = self.dataSouce[indexPath.row];
        if (![model.ACCOUNTID isEqualToString:ZJUSERINFORMODEL.ORG_ACCOUNT.ID]) {
            StudyRankingPersonInformationViewController *vc = [[StudyRankingPersonInformationViewController alloc] init];
            vc.personModel = model;
            vc.myModel = self.myModel;
            [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
        }
        
    }
}




#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_NavBarHeight, self.view.bounds.size.width, self.view.bounds.size.height - LP_TabBarHeight - LP_NavBarHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor themeGrayColor];
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [[TenGrayShadowView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 10 * [UIScreen zj_screenHScale])];
    }
    return _tableView;
}


-(NSMutableArray *)dataSouce
{
    if (!_dataSouce) {
        _dataSouce = [NSMutableArray array];
    }
    return _dataSouce;
}

- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}


@end
